package com.yiren.dbaa.module.practice_set.map.presenter;

import android.app.Activity;

import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.StationParamConfigBean;
import com.yiren.backstage.config.bean.device.UserParamConfigBean;
import com.yiren.backstage.currency.entity.MapEntity;
import com.yiren.backstage.currency.utils.FileUtil;
import com.yiren.backstage.db.dao.controller.GradeDbController;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.constant.AddressConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.module.param_config.api.ParamConfigApi;
import com.yiren.dbaa.module.practice_set.map.contract.MapHomeContract;
import com.yiren.dbaa.module.practice_set.map.model.MapHomeModel;
import com.yiren.dbaa.view.dialog.LoadDialog;
import com.yiren.dbaa.view.toast.MessageBox;

import java.io.File;
import java.util.List;

/**
 * @author lwb
 * @date : 2022/11/24 14:41
 * @description: 地图首页
 */
public class MapHomePresenter implements MapHomeContract.Presenter {

    private Activity activity;
    private MapHomeContract.Model mapHomeModel;
    private UserParamConfigBean userParamConfig;

    public MapHomePresenter(Activity activity) {
        this.activity = activity;
        mapHomeModel = new MapHomeModel(activity);
        userParamConfig = LoadConfigApi.loadUserParamConfig(activity);
    }

    @Override
    public void loadMapList(MapHomeContract.View view) {
        //从服务器获取
        activity.runOnUiThread(() -> LoadDialog.show(activity, "正在加载..."));
        mapHomeModel.getMapFromServer(userParamConfig.getUserInfo().getpUserId().toString(), false, new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(activity);
                List<MapEntity> serverMaps = (List<MapEntity>) result;
                //获取本地列表
                List<MapEntity> localMaps = UserDbController.getInstance(false).queryAll(MapEntity.class);
                if (null == serverMaps) {
                    view.showMap(localMaps);
                    return;
                }
                if (null == localMaps) {
                    view.showMap(serverMaps);
                    return;
                }
                //数据合并去重，服务器与本地数据都存在以本地数据为准
                view.showMap(mapHomeModel.lineHomeDataMerge(localMaps, serverMaps));
                super.success(result);
            }

            @Override
            public void fail(String error) {
                LoadDialog.dismiss(activity);
                MessageBox.show(activity, error);
                //获取失败加载本地
                List<MapEntity> localMaps = GradeDbController.getInstance(false).queryAll(MapEntity.class);
                for (MapEntity mapEntity : localMaps) {
                    mapEntity.setDownLoad(true);
                }
                view.showMap(localMaps);
            }
        });
    }

    @Override
    public void loadSiteMap(MapEntity map, MapHomeContract.View view) {
        LoadDialog.show(activity, "正在下载数据...");
        mapHomeModel.downloadSiteMap(map.getSiteCode(), userParamConfig.getUserInfo().getpUserId(), map.getMapVer(), new CommonCallBack() {
            @Override
            public void success(Object result) {
                LoadDialog.dismiss(activity);
                if (UserDbController.getInstance(false).insertOrReplace(map)) {
                    //通知视图
                    LoadDialog.dismiss(activity);
                    MessageBox.MessageAndVoiceModel(activity, "下载地图成功");
                    view.refreshMapList();
                    if (userParamConfig.getSite().getSiteCode().equals(map.getSiteCode())) {
                        StationParamConfigBean stationParamConfig = LoadConfigApi.loadStationParamConfig(activity);
                        stationParamConfig.setBaseStationId(map.getBaseId());
                        ParamConfigApi.saveStationParamConfig(activity, stationParamConfig);
                    }
                } else {
                    MessageBox.MessageAndVoiceModel(activity, "下载地图失败");
                }
            }

            @Override
            public void fail(String error) {
                LoadDialog.dismiss(activity);
                MessageBox.MessageAndVoiceModel(activity, "下载地图失败");
                MessageBox.show(activity, error);
            }
        });
    }

    @Override
    public void deleteMap(MapEntity map, CommonCallBack callBack) {
        UserParamConfigBean userParamConfigBean = LoadConfigApi.loadUserParamConfig(activity);
        //删除文件夹
        File file = new File(AddressConstant.App_Main_Directory() + userParamConfigBean.getUserInfo().getId() + "/map/" + map.getSiteCode());
        File to = new File(file.getAbsolutePath() + System.currentTimeMillis());
        file.renameTo(to);
        FileUtil.delFolder(to.getAbsolutePath());
        //清除本地记录
        if (UserDbController.getInstance(false).delete(map)) {
            callBack.success("");
        }
    }
}
